"""

import os,json
from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from myAPI.imgToStr import cnOcr_imgToStr


imgExt = ['.bmp', '.gif', '.jpg', '.pic', '.png', '.tif', '.jpeg', '.php',\
          '.BMP', '.GIF', '.JPG', '.PIC', '.PNG', '.TIF', '.JPEG', '.PHP']

def upfile(request):
    s = ""
    if request.method == "POST":
        filename = up_file(request)        
        s = cnOcr_imgToStr(filename, '') #图片转换为文字1
    return render(request, 'blog/upfile.html', context=locals()) 

# 上传文件
def up_file(request):
    upFile = request.FILES.get("upfile", "")
    if not upFile:
        print("Open File Error.")
        return HttpResponse("Open File Error.")
    name = upFile.name
    if os.path.splitext(name)[1] not in imgExt:
        print("Open IMG Error.")
        return HttpResponse("Open IMG Error.")
    filename = "%s/upfile/%s" % (os.path.abspath(os.path.dirname(__file__)), name)
    f = open(filename, 'wb+')
    for chunk in upFile.chunks():
        f.write(chunk)
    f.close()
    print("upfile Seccess.")
    return filename    

#android手机应用程序  图片转换为文字1
@csrf_exempt
def upload(request):
    '''android手机按钮 上传文件'''
    mag = "欢迎使用"
    if request.method == "POST":
        filename = up_file(request)        
        s = cnOcr_imgToStr(filename, '') #图片转换为文字1
        print("img to str Seccess.")
        return HttpResponse(s)
    return HttpResponse(mag)

#android手机应用程序  图片转换为文字2
@csrf_exempt 
def imgtostr(request):
    '''android手机按钮 上传转换  ''' 
    mag = "欢迎使用"
    if request.method == "POST":
        filename = up_file(request) 
        s = cnOcr_imgToStr(filename, 'naive_det') #图片转换为文字2
        print("img to str Seccess. naive_det")
        return HttpResponse(s)       
    return HttpResponse(mag)
"""
